package org.mics.cyy.backUser.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.mics.cyy.backUser.entity.RoleMenuDO;
import org.mics.cyy.backUser.repository.RoleMenuRepository;
import org.mics.cyy.backUser.request.RoleMenuRequest;
import org.mics.cyy.backUser.service.RoleMenuService;
import org.springframework.stereotype.Service;

/**
 * 角色菜单关系
 * @author mics
 * @date 2023-10-9
 * @version  1.0
 */
@Service
public class RoleMenuServiceImpl implements RoleMenuService{
	@Resource
	private RoleMenuRepository roleMenuRepository;

	@Override
	@Transactional
	public void grant(RoleMenuRequest roleMenuRequest) {
		//删除原来的关系
		List<RoleMenuDO> roleMenuDOs = roleMenuRepository.findByRoleId(roleMenuRequest.getRoleId());
		roleMenuRepository.deleteAll(roleMenuDOs);
		//保存现有的关系
		for(String menuId:roleMenuRequest.getMenuIds()) {
			RoleMenuDO roleMenuDO = new RoleMenuDO();
			roleMenuDO.setRoleId(roleMenuRequest.getRoleId());
			roleMenuDO.setMenuId(menuId);
			roleMenuRepository.save(roleMenuDO);
		}
	}
	
	
}
